﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ArrivalRegisterAcceptance : EFViewCmes
    {
        public ArrivalRegisterAcceptance(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdMaterialCode.IsReadOnly = true;
                    this.fdMaterialName.IsReadOnly = true;
                    this.fdMetric.IsEnabled = false;
                    this.fdSpecModel.IsReadOnly = true;
                    this.fdNumber.IsReadOnly = true;
                    this.fdQuality.IsReadOnly = true;
                    this.fdPlanNum.IsReadOnly = true;
                    this.fdActualNum.IsReadOnly = true;
                    this.fdReceiveSum.IsReadOnly = true;
                    this.fdValidatePeople.IsEnabled = false;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (!ValidateField())
            {
                return;
            }

            base.OnCommitButtonClick("ZMCMF1", "acceptance", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 验收时的校验
        /// </summary>
        /// <returns></returns>
        public bool ValidateField()
        {
            string fdValidateNum = String.IsNullOrEmpty(this.fdValidateNum.Value.ToString())? "0" : this.fdValidateNum.GetDataValue().ToString();
            string fdActualNum= String.IsNullOrEmpty(this.fdActualNum.Value.ToString())? "0" : this.fdActualNum.GetDataValue().ToString();
            if (Double.Parse(fdValidateNum) > Double.Parse(fdActualNum))
            {
                ValidateMessagePopup("验收数量不得大于到货数量！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }


        private void fdValidateNum_LostFocus(object sender, RoutedEventArgs e)
        {
            double sum = 0;
            double num = 1;
            Double.TryParse(this.fdReceiveSum.Value.ToString(), out sum);
            Double.TryParse(this.fdActualNum.Value.ToString(), out num);
            String fdValidateNum = this.fdValidateNum.Value.ToString();

            double jg = sum / num;

            double sqsl = 0;
            if (!(fdValidateNum == null || fdValidateNum.Equals("")))
            {
                Double.TryParse(this.fdValidateNum.Value.ToString(), out sqsl);
            }
            double je = jg * sqsl;
            this.fdPrice.Value = jg.ToString();
            this.fdValidateSum.Value = je.ToString();
        }
    }
}
